import {defineStore} from 'pinia';

export const useGepSupplyServiceStore = defineStore('gepSupplyService', {
    state() {
        return {
            // 农业产值
            inAgricultural: 0,
            // 牧业产值
            inAnimalHusbandry: 0,
            // 林业产值
            inForestry: 0,
            // 渔业产值
            inFishery: 0,
            // 清洁能源发电量
            inEcologicalEnergy: 0,
            // 水资源
            inWaterResources: 0,
            // 当地水价
            inLocalWaterPrice: 0,
            // 当地电价
            inElectricityPrice: 0,

            labelInAgricultural: '农业产值(万元)',        // 农业产值
            labelInAnimalHusbandry: '牧业产值(万元)',    // 牧业产值
            labelInForestry: '林业产值(万元)',           // 林业产值
            labelInFishery: '渔业产值(万元)',            // 渔业产值
            labelInEcologicalEnergy: '清洁能源发电量(万千瓦时)',   // 生态能源
            labelInWaterResources: '水资源（万立方米）',     // 水资源
            labelInLocalWaterPrice: '当地水价（元/立方米）',    // 当地水价
            labelInElectricityPrice: '当地电价（元/千瓦时）',    // 当地电价
        };
    },

    actions: {
        reset() {
            this.inAgricultural = 0;
            this.inAnimalHusbandry = 0;
            this.inForestry = 0;
            this.inFishery = 0;
            this.inEcologicalEnergy = 0;
            this.inWaterResources = 0;
            this.inLocalWaterPrice = 0;
            this.inElectricityPrice = 0;
        }
    },
    getters: {
        // 可以在这里添加计算属性
        outGepSupplyService(): number {
            return this.outCalcMaterialSupply + this.outEcologicalEnergy + this.outCalcWaterResources;
        },
        outCalcMaterialSupply(): number {
            // MaterialSupplyValue计算公式 物质供给（万元）=农业产值+林业产值+畜牧业产值+渔业产值
            return this.inAgricultural + this.inAnimalHusbandry + this.inForestry +
                this.inFishery
        },
        outEcologicalEnergy(): number {
            // 清洁能源（万元）=清洁能源发电量(万千瓦时)*电价（元/千瓦时）
            return this.inEcologicalEnergy * this.inElectricityPrice
        },
            // 水资源（万元）=水资源总量（万立方米）*水价（元/立方米）
        outCalcWaterResources(): number {
            return this.inWaterResources * this.inLocalWaterPrice
        },
        // bar 图的数据
        barData1() {
            return [
                { value: this.outEcologicalEnergy, name: '生态能源', itemStyle: { color: '#33caca' } },
                { value: this.outCalcWaterResources, name: '水资源', itemStyle: { color: '#33caca' } },
                { value: this.outCalcMaterialSupply, name: '农牧林渔', itemStyle: { color: '#33caca' } },
            ];
        }
    },
});

// 调节服务的八项数据更新由监听gepRegion实现，实现方式：直接赋值修改
export const useGepRegulationServiceStore = defineStore('gepRegulationService', {
    state() {
        return {
            // 水源涵养 outWaterSourceConservation
            inWaterSourceConservation: 0,
            // 土壤保持 outSoilConservation
            inSoilConservation: 0,
            // 负氧离子 outNegativeOxygenIons
            inNegativeOxygenIons: 0,
            // 空气净化 outAirPurification
            inAirPurification: 0,
            // 水质净化 outWaterPurification
            inWaterPurification: 0,
            // 固碳价值 outCarbonSequestration
            inCarbonSequestration: 0,
            // 释氧价值 outOxygenRelease
            inOxygenRelease: 0,
            // 气候调节 outClimateRegulation
            inClimateRegulation: 0,
            // 洪水调蓄 outCalcFloodRegulation
            inFloodRegulation: 0,


            // 保护区面积（万平方千米）
            inProtectedArea: 0,
            // 保育成本（元/平方千米）
            inProtectedCost: 0,
            // 噪声消减量（万dB）
            inNoiseReductionAmount: 0,
            // 噪声降低成本（元/dB）
            inNoiseCost: 0,

            labelInWaterSourceConservation: '水源涵养',
            labelInSoilConservation: '土壤保持',
            labelInNegativeOxygenIons: '负氧离子',
            labelInAirPurification: '空气净化',
            labelInWaterPurification: '水质净化',
            labelInCarbonSequestration: '固碳价值',
            labelInOxygenRelease: '释氧价值',
            labelInClimateRegulation: '气候调节',
            labelInFloodRegulation: '洪水调蓄',

            labelInProtectedArea: '保护区面积（万平方千米）',
            labelInProtectedCost: '保育成本（元/平方千米）',
            labelInNoiseReductionAmount: '噪声消减量（万dB）',
            labelInNoiseCost: '噪声降低成本（元/dB）',
        };
    },
    // persist: true, // 启用持久化
    actions: {
        reset() {
            this.inWaterSourceConservation = 0;
            this.inSoilConservation = 0;
            this.inNegativeOxygenIons = 0;
            this.inAirPurification = 0;
            this.inWaterPurification = 0;
            this.inCarbonSequestration = 0;
            this.inOxygenRelease = 0;
            this.inClimateRegulation = 0;
            this.inFloodRegulation = 0;

            this.inProtectedArea = 0;
            this.inProtectedCost = 0;
            this.inNoiseReductionAmount = 0;
            this.inNoiseCost = 0;
        },
    },
    getters: {
        outGepRegulationService(): number {
            // + this.inNegativeOxygenIons + this.inOxygenRelease
            return this.inWaterSourceConservation +
                this.inSoilConservation  +
                this.inAirPurification + this.inWaterPurification +
                this.inCarbonSequestration  +
                this.inClimateRegulation + this.inFloodRegulation +
                //  上面是模型提供的七项数据
                this.outCalcBiodiversityProtection +
                this.outCalcNoiseReduced;
        },
        outCalcBiodiversityProtection(): number {
            // 生物多样性保护（万元）= 保护区面积（平方千米）*保育成本（万元/平方千米）
            return this.inProtectedArea * this.inProtectedCost ;
        },
        outCalcNoiseReduced(): number {
            // 噪声消减（万元）=生态系统噪声消减量（万dB）*噪声降低成本（元/dB）
            return this.inNoiseReductionAmount * this.inNoiseCost;
        },
    },
});

export const useGepCulturalServiceStore = defineStore('gepCulturalService', {
    state() {
        // 9个参数
        return {
            // 旅游收入
            inTourismRevenue: 0,
            // 城镇人均住房面积 inHousingAreaPerCapita
            inHousingAreaPerCapita: 0,
            // 当地房价（元/平方米） inHousePrice
            inDisposableIncome: 0,
            // 总酒店客房数（间） inHotelRooms
            inHotelRooms: 0,
            // 客房价格（元/间）
            inRoomPrice: 0,
            // 年入住率（%） inOccupancyRate
            inOccupancyRate: 0,
            // 城镇常住人口（万人） inUrbanPopulation
            inUrbanPopulation: 0,
            // 日休闲游憩时间（时/天人）  inLeisureTime
            inLeisureTime: 0,
            // 人均工资（元/人时）  inAverageIncome
            inAverageIncome: 0,

            labelInTourismRevenue: '旅游收入',
            labelInDisposableIncome: '城镇居民可支配收入',
            labelInUrbanPopulation: '常住城镇人口',
            labelInGreenSpacePerCapita: '人均公园绿地面积',
            labelInHotelRooms: '总酒店客房数',
            labelInRoomPrice: '客房价格',
            labelInHousingAreaPerCapita: '人均住房面积',
        };
    },
    actions: {
        reset() {
            this.inTourismRevenue = 0;
            this.inHousingAreaPerCapita = 0;
            this.inDisposableIncome = 0;
            this.inHotelRooms = 0;
            this.inRoomPrice = 0;
            this.inOccupancyRate = 0;
            this.inUrbanPopulation = 0;
            this.inLeisureTime = 0;
            this.inAverageIncome = 0;
        },
    },
    getters: {
        // 计算属性示例
        outGepCulturalService(): number {
            return this.outTourismHealth + this.outCalcLeisureRecreation + this.outCalcLandscapeAppreciation;
        },
        outTourismHealth(): number {
            // 旅游康养=旅游收入
            const result = this.inTourismRevenue;
            return Number(result.toFixed(4));
        },
        outCalcLeisureRecreation(): number {
            // 休闲游憩(万元) = 城镇常住人口 * 日休闲游憩时间*365*人均工资
            const result = this.inUrbanPopulation * this.inLeisureTime * 365 * this.inAverageIncome;
            return Number(result.toFixed(4));
        },
        outCalcLandscapeAppreciation(): number {
            // 景观增值（万元）=城镇人均住房面积*当地房价*0.08*城镇常住人口+
            // 酒店客房总数*客房价格*(客房价格/228.52-1)*年入住率*365/10000
            const result = this.inHousingAreaPerCapita * this.inDisposableIncome * 0.08 * this.inUrbanPopulation
                + this.inHotelRooms * this.inRoomPrice * (this.inRoomPrice / 228.52 - 1) * this.inOccupancyRate * 365 / 10000;
            return Number(result.toFixed(4));
        }
    },
});

// import { useHomepageStore } from '@/stores/homepage';
// const homepageStore = useHomepageStore();

export const useGepDataStore = defineStore('gepDataStore', {
    state() {
        return {
            // 地区选择器的内容
            selectedArea:'',
            selectedYear:0,

            gepResultArea: '',
            gepCity: '市',
            gepDistrict: '总计',
            gepTown: '总计',
            // gepYear: new Date().getFullYear() - 1, // 默认值2024,
            gepYear: new Date().getFullYear() - 1, // 默认值2024,
            gepValue: 0,
            // 是否选择完地址
            isSelected: false,
            // gep数据是否计算成功
            isSubmitted: false,
            count: 0,
            // 是否显示柱子的标签
            showDataLabel:true,
            gepMapData:[]
        }
    },
    actions: {
        reset() {
            this.selectedArea = '';
            this.selectedYear = 0;
            this.gepResultArea = '';
            this.gepCity = '市';
            this.gepDistrict = '总计';
            this.gepTown = '总计';
            this.gepYear = new Date().getFullYear() - 1; // 默认值2024
            this.gepValue = 0;
            this.isSelected = false;
            this.isSubmitted = false;
            this.count = 0;

            // 调用 useGepRegulationServiceStore 的 reset 方法
            const gepRegulationStore = useGepRegulationServiceStore();
            gepRegulationStore.reset(); // 调用 reset 方法
            const gepSupplyServiceStore = useGepSupplyServiceStore();
            gepSupplyServiceStore.reset(); // 调用 reset 方法
            const gepCulturalServiceStore = useGepCulturalServiceStore();
            gepCulturalServiceStore.reset(); // 调用 reset 方法
        },
        // resetGEP(){
        //     // 调用 useGepRegulationServiceStore 的 reset 方法
        //     const gepRegulationStore = useGepRegulationServiceStore();
        //     gepRegulationStore.reset(); // 调用 reset 方法
        //     const gepSupplyServiceStore = useGepSupplyServiceStore();
        //     gepSupplyServiceStore.reset(); // 调用 reset 方法
        //     const gepCulturalServiceStore = useGepCulturalServiceStore();
        //     gepCulturalServiceStore.reset(); // 调用 reset 方法
        // }

    },
    getters: {
        outGepResultArea():any {
            // 检查 gepDistrict 和 gepTown 的值是否为 "总计"
            if (this.gepDistrict === "总计") {
                return this.gepCity; // 如果 gepDistrict 为总计，返回 this.gepCity
            } else if (this.gepTown === "总计") {
                return this.gepCity + this.gepDistrict; // 如果 gepTown 为总计，返回 this.gepDistrict
            } else {
                // 否则返回所有的值
                return this.gepCity + this.gepDistrict + this.gepTown;
            }
        },

        gepResultAreaOne():any {
            // 检查 gepDistrict 和 gepTown 的值是否为 "总计"
            if (this.gepDistrict === "总计") {
                return this.gepCity; // 如果 gepDistrict 为总计，返回 this.gepCity
            } else if (this.gepTown === "总计") {
                return this.gepDistrict; // 如果 gepTown 为总计，返回 this.gepDistrict
            } else {
                // 否则返回镇的名字
                return this.gepTown;
            }
        },
        // gepMapData():any {
        //     return [
        //         { value: this.outEcologicalEnergy, name: '生态能源', itemStyle: { color: '#33caca' } },
        //         { value: this.outCalcWaterResources, name: '水资源', itemStyle: { color: '#33caca' } },
        //         { value: this.outCalcMaterialSupply, name: '农牧林渔', itemStyle: { color: '#33caca' } },
        //     ];
        // }
    }
})
